package cn.tedu.eguser.controller;


import cn.tedu.eguser.base.response.JsonResult;
import cn.tedu.eguser.base.response.StatusCode;
import cn.tedu.eguser.mapper.UserMapper;
import cn.tedu.eguser.pojo.dto.UserAddParam;
import cn.tedu.eguser.pojo.vo.UserListVO;
import cn.tedu.eguser.pojo.dto.UserUpdateParam;
import cn.tedu.eguser.pojo.entiy.User;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user")
@Slf4j
@Tag(name = "01-用户管理接口")
@Validated
public class UseController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("add")
    @Operation(summary = "添加用户接口")
    @ApiOperationSupport(order = 10)
    public JsonResult addUser(@RequestBody @Validated UserAddParam userAddParam){
        log.debug("传入的参数："+userAddParam);
        String username = userAddParam.getUsername();
        int count = userMapper.selectByUsername(username);
        if(count>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int row= userMapper.insert(user);
        if(row>0){
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @GetMapping("list")
    @Operation(summary = "查询用户列表接口")
    @ApiOperationSupport(order = 20)
    public JsonResult selectUserList(){
        List<UserListVO> list = userMapper.selectUserList();
        return JsonResult.ok(list);
    }

    @PostMapping("del")
    @Operation(summary = "删除用户接口")
    @ApiOperationSupport(order = 30)
    public JsonResult deleteUser(@Range(min=1,max=100,message="id必须在1-100之间") Long id){
        int row = userMapper.deleteById(id);
        if(row>0){
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }

    @PostMapping("update")
    @Operation(summary = "更新用户接口")
    @ApiOperationSupport(order = 40)
    public JsonResult updateUser(@RequestBody UserUpdateParam userUpdateParam){
        System.out.println("传入的参数："+userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        System.out.println(user);
        user.setUpdateTime(new Date());
        System.out.println(user);
        int row = userMapper.updateById(user);
        System.out.println(row);
        if(row>0){
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
}
